﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class WarehouseS : WarehouseSI
    {
        private readonly WmssystemContext myDb;

        public WarehouseS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }
        public Task<List<Sys_warehouse>> Wqu()
        {
            var query = myDb.Sys_warehouse.AsQueryable();
            query = query.Where(c => c.w_wisdelete == 1);
            return query.ToListAsync();
        }
        //分页模糊查询
        public Task<List<Sys_warehouse>> FenYe(int pageSize, int currentPage, string key = null, int? Status = null)
        {
            var query = myDb.Sys_warehouse.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(key))
            {
                query = query.Where(c => c.w_wnumber.Contains(key) || c.w_wname.Contains(key));
            }
            if (Status.HasValue)
            {
                query = query.Where(c => c.w_wstatus == Status);
            }
            query = query.Where(c => c.w_wisdelete == 1);
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }
        //修改状态
        public async Task<int> UpdStatus(int id, int status)
        {
            var entity = await myDb.Sys_warehouse.FindAsync(id);
            // 更新实体的状态
            entity.w_wstatus = status;
            // 标记实体为已修改状态
            myDb.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDb.SaveChangesAsync();
        }
        //添加
        public Task<int> Add(Sys_warehouse add)
        {
            myDb.Sys_warehouse.Add(add);
            return myDb.SaveChangesAsync();
        }
        //修改
        public async Task<int> Upd(Sys_warehouse upd)
        {
            var existingOrder = await myDb.Sys_warehouse.FirstOrDefaultAsync(o => o.w_wid == upd.w_wid);
            // 检查订单是否存在
            if (existingOrder == null)
            {
                return 0; // 表示未找到要更新的订单
            }
            existingOrder.w_wnumber = upd.w_wnumber;
            existingOrder.w_wname = upd.w_wname;
            existingOrder.w_wcapacity = upd.w_wcapacity;
            existingOrder.w_wdid = upd.w_wdid;
            existingOrder.w_wcharge = upd.w_wcharge;
            existingOrder.w_wphone = upd.w_wphone;
            existingOrder.w_waddress = upd.w_waddress;
            existingOrder.w_wsort = upd.w_wsort;
            existingOrder.w_wstatus = upd.w_wstatus;
            existingOrder.w_wremark = upd.w_wremark;

            return await myDb.SaveChangesAsync();
        }
        //逻辑删除
        public async Task<int> UpdIsDelete(int id, int isDelete)
        {
            var entity = await myDb.Sys_warehouse.FindAsync(id);
            // 更新实体的删除状态
            entity.w_wisdelete = isDelete;
            // 标记实体为已修改状态
            myDb.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDb.SaveChangesAsync();
        }
    }
}
